﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jemer.Domain;
using Jemer.Data;
using Jemer.Data.Infrastructure;
using Jemer.Domain.Entities;
using Jemer.Data.Repositories;

namespace Jemer.Service
{
    
    public interface IProductTypeService
    {
        IQueryable<ProductType> GetProductTypes();
        ProductType GetProductType(int id);
        void CreateProductType(ProductType productType);
        void DeleteProductType(int id);
        void SaveProductType();
    }
    
    public class ProductTypeService : IProductTypeService
    {
        private readonly IProductTypeRepository productTypeRepository;
        private readonly IUnitOfWork unitOfWork;
        public ProductTypeService(IProductTypeRepository productTypeRepository, IUnitOfWork unitOfWork)
        {
            this.productTypeRepository = productTypeRepository;
            this.unitOfWork = unitOfWork;
        }  
        #region IProductTypeService Members

        public IQueryable<ProductType> GetProductTypes()
        {
            var productTypes = productTypeRepository.GetAll();
            return productTypes;
        }

        public ProductType GetProductType(int id)
        {
            var productType = productTypeRepository.GetById(id);
            return productType;
        }

        public void CreateProductType(ProductType productType)
        {
            productTypeRepository.Add(productType);
            SaveProductType();
        }

        public void DeleteProductType(int id)
        {
            var productType = productTypeRepository.GetById(id);
            productTypeRepository.Delete(productType);
            SaveProductType();
        }

        public void SaveProductType()
        {
            unitOfWork.Commit();
        }

        #endregion
    }
    
    
}